package com.zxk.service.impl;

import com.sun.jna.platform.win32.WinDef;
import com.zxk.constant.JNAConstants;
import com.zxk.constant.PicConstants;
import com.zxk.enums.Coord;
import com.zxk.service.AbstractMyTask;
import com.zxk.util.FuzzyPicUtil;
import com.zxk.util.KeyUtil;
import com.zxk.util.MouseUtil;
import com.zxk.util.ThreadUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 物品卖银币任务
 *
 * @author: 舟小亢
 * @date: 2023-08-27 15:09
 */
public class SellSilverCoinImpl extends AbstractMyTask {

    private static Logger LOG = LoggerFactory.getLogger(SellSilverCoinImpl.class);


    public SellSilverCoinImpl(WinDef.HWND hwnd_0, WinDef.HWND hwnd_1, WinDef.HWND hwnd_2) {
        super(hwnd_0, hwnd_1, hwnd_2);
    }

    @Override
    public String getTaskName() throws Exception {
        return "物品卖银币";
    }

    @Override
    public boolean execute() throws Exception {
        LOG.warn("【物品卖银币任务】开始！");

        // 查找并点击商城
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_商城)) {
            return false;
        }
        ThreadUtil.sleep1();

        if (!FuzzyPicUtil.findPic(hwnd_0, PicConstants.物品卖银币任务_商会已选择,3,0.95).isFinded()) {
            if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.物品卖银币任务_商会未选择)) {
                LOG.error("【物品卖银币任务】商会图标未找到！退出任务");
                return false;
            }
        }

        // 点击出售物品
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.物品卖银币任务_我要出售)) {
            return false;
        }

        // 选择出售第一个物品
        MouseUtil.lClickRandom(hwnd_1,
                Coord.SELL_FIRST_XY.getX(),
                Coord.SELL_FIRST_XY.getY());
        MouseUtil.lClickRandom(hwnd_1,
                Coord.SELL_FIRST_XY.getX(),
                Coord.SELL_FIRST_XY.getY());

        LOG.warn("【物品卖银币任务】开始售卖！");

        return true;
    }

    @Override
    public boolean checkTask() throws Exception {
        // 循环点击出售，直到完成
        for (int i = 0; i < 50; i++) {
            LOG.info("【物品卖银币任务】售卖第{}次", i + 1);
            FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.物品卖银币任务_出售,3);
            // 点击出售
            if (!FuzzyPicUtil.findPic(hwnd_0, PicConstants.物品卖银币任务_价格涨跌,3).isFinded()) {
                LOG.info("【物品卖银币任务】未找到价格涨跌，任务已完成，退出！");
                KeyUtil.kClick(hwnd_1, JNAConstants.VK_ESCAPE);
                return true;
            }
        }
        LOG.info("【物品卖银币任务】完成！");
        return true;
    }
}
